package cn.juque.datapipeline.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.datapipeline.entity.DatabaseInfo;
import cn.juque.datapipeline.service.IDatabaseInfoService;
import cn.juque.datapipeline.vo.databaseinfo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>DatabaseInfoController</li>
 * </ul>
 * @date 2023-03-31 09:51:27
 **/
@Slf4j
@Controller
@RequestMapping("/dpp/database")
public class DatabaseInfoController extends BaseController {

    @Resource
    private IDatabaseInfoService databaseInfoService;

    /**
     * 查询
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public BaseResponseVO<List<DatabaseInfoListVO>> list(@RequestBody BaseRequestVO<DatabaseInfoSearchVO> request) {
        DatabaseInfoSearchVO searchVO = super.validatorUtil.checkNotNull(request);
        PageInfo pageInfo = request.getPageInfo();
        super.validatorUtil.checkPageInfo(pageInfo);
        DatabaseInfo databaseInfo = new DatabaseInfo();
        BeanUtil.copyProperties(searchVO, databaseInfo);
        List<DatabaseInfo> list = this.databaseInfoService.listInfoWithPage(pageInfo, databaseInfo);
        List<DatabaseInfoListVO> voList = BeanUtil.copyToList(list, DatabaseInfoListVO.class);
        return new BaseResponseVO<>(MessageEnum.OK, voList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/detail")
    @ResponseBody
    public BaseResponseVO<DatabaseInfoDetailVO> detail(String id) {
        Assert.notEmpty(id);
        DatabaseInfo detail = this.databaseInfoService.detail(id);
        DatabaseInfoDetailVO detailVO = new DatabaseInfoDetailVO();
        BeanUtil.copyProperties(detail, detailVO);
        return new BaseResponseVO<>(MessageEnum.OK, detailVO);
    }

    /**
     * 新增
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public BaseResponseVO<Boolean> add(@RequestBody BaseRequestVO<DatabaseInfoAddVO> request) {
        super.validatorUtil.fastValidate(request);
        DatabaseInfoAddVO addVO = request.getParam();
        DatabaseInfo databaseInfo = new DatabaseInfo();
        BeanUtil.copyProperties(addVO, databaseInfo);
        this.databaseInfoService.saveInfo(databaseInfo);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 更新
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public BaseResponseVO<Boolean> edit(@RequestBody BaseRequestVO<DatabaseInfoUpdateVO> request) {
        super.validatorUtil.fastValidate(request);
        DatabaseInfoUpdateVO updateVO = request.getParam();
        DatabaseInfo databaseInfo = new DatabaseInfo();
        BeanUtil.copyProperties(updateVO, databaseInfo);
        this.databaseInfoService.updateInfo(databaseInfo);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 删除
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    public BaseResponseVO<Boolean> delete(@RequestBody BaseRequestVO<DatabaseInfoDeleteVO> request) {
        super.validatorUtil.fastValidate(request);
        List<String> idList = request.getParam().getIdList();
        this.databaseInfoService.deleteInfo(idList);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }
}
